package com.navinfo.opentsp.qingqi.trackoptimize.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 获取Mongo数据库中按周分表的表名_工具类
 * wangshuai 2017/02/16
 */
public class MongoUtil {

    protected static final Logger logger = LoggerFactory.getLogger(MongoUtil.class);

//=======================原hot表获取表名方法====================================
    //获取要查询时间段内的表名集合
     @Deprecated
     public static List<String> getTableList(String firstName, String beginDate, String endDate) throws JsonProcessingException {
        //计算共需查询那些表
        List tList = new ArrayList();

        Date date = (DateUtil.parseDate(beginDate, DateUtil.time_pattern));
        Date enddate = (DateUtil.parseDate(endDate, DateUtil.time_pattern));
        String kname = getTableName(date);
        tList.add(firstName + kname);
        String jname = getTableName(enddate);
        if(!"_hot".equals(kname)){
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

            Calendar calendar2 = Calendar.getInstance();
            calendar2.setFirstDayOfWeek(Calendar.MONDAY);
            if ("_hot".equals(jname)){
                calendar2.setTime(new Date());
            }else {
                calendar2.setTime(enddate);
            }
            calendar2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

            while (calendar.before(calendar2)){
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY ){
                    tList.add(firstName + "_" + new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
                }
            }
        }
        if ("_hot".equals(jname)&&!"_hot".equals(kname)) {
            tList.add(firstName + jname);
        }
        System.out.println(JsonUtil.toJson(tList));
        return tList;
    }


    @Deprecated
    //获取表名后缀(包括当天数据表和历史数据表)
    public static String getTableName(Date date) {
        //如果时间是今天则只返回实时表
        if (new Date().getTime() <= date.getTime()) {
              date = new Date();
        }
        if (DateUtil.formatDate(new Date()).equals(DateUtil.formatDate(date))) {
           return "_hot";
        }
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
        //System.out.println("getTableName表名后缀：" + monday);
        return "_" + monday;
    }






    //=============================新trip表（pb版）获取表名方法====================================
    //获取要查询时间段内的表名集合(trip表)
    public static List<String> getTripTableList(String firstName, String beginDate, String endDate){
        //计算共需查询那些表
        List tList = new ArrayList();

        Date date = (DateUtil.parseDate(beginDate, DateUtil.time_pattern));
        Date enddate = (DateUtil.parseDate(endDate, DateUtil.time_pattern));
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setFirstDayOfWeek(Calendar.MONDAY);
        if (enddate.getTime()>=new Date().getTime()){
            calendar2.setTime(new Date());
        }else {
            calendar2.setTime(enddate);
        }
        while (calendar.before(calendar2)){
            if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY ){
                tList.add(firstName + "_" + new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
            }
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }

        //System.out.println(JsonUtil.toJson(tList));
        return tList;
    }


    //获取表名后缀
    public static String getTripTableName(Date date) {
        //如果时间大于今天设置为当前
        if (new Date().getTime() <= date.getTime()) {
            date = new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
        //System.out.println("getTripTableName：" + monday);
        return "_" + monday;
    }

    public static void main(String[] args) throws JsonProcessingException {
        //MongoUtil.getTripTableList("abc", "2017-03-12 10:00:00", "2017-03-12 10:30:30");
        MongoUtil.getTripTableName(new Date(1486051200000l));
    }

}